package com.ece;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import com.ece.beans.Commande;
import com.ece.beans.items.DVD;
import com.ece.beans.items.Item;
import com.ece.beans.items.Livre;
import com.ece.beans.users.Utilisateur;

@Stateless
public class CommandeBusinessController implements CommandeBusinessControllerRemote {

	public CommandeBusinessController() {

	}

	public void validerCommande(List<Item> items, Utilisateur user)
	throws Exception {
		Commande commande = new Commande(user);
		String ref = commande.getUser().getEmail() + Long.toString(commande.getDate().getTime());
		commande.setReference(ref);
		commande.setItems(items);
		commande.validerCommande();
	}

	public void passerCommande(List<String> items, Utilisateur user) throws Exception {
		Commande commande = new Commande(user);
		List<Item> list = new ArrayList<Item>();
		for(String ref : items) {
			Item item = null;
			if(ref.charAt(0) == 'D')
				item = new DVD(ref, 0, "", 0);
			else if(ref.charAt(0) == 'L')
				item = new Livre(ref, 0, "", "");
			list.add(item);
		}
		commande.setItems(list);
		commande.validerCommande();
	}
}
